package Func;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
* Created with IntelliJ IDEA.
* Description:
* User: 38917
* Date: 2025-02-16
* Time: 02:16
*/public class DelFunc implements Func{
    @Override
    public void work(BookList bookList) {
        int curSize = bookList.getUsedSize();
        if (curSize == 0){
            System.out.println("书架为空,不能删除");
        }
        Scanner in = new Scanner(System.in);
        System.out.println("请输入你要删除的书名");
        String name = in.nextLine();
        int index = -1;
        for (int i = 0; i < curSize; i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)){
                 index = i;
            }
        }
        if (index == -1){
            System.out.println("没有你要删除的图书");
        }else {
            for (int i = index; i < curSize - 1; i++) {
                Book book = bookList.getBooks(i + 1);
                bookList.setBooks(i,book);
            }
            System.out.println("删除成功" + "《" + name + "》");
        }
        bookList.setUsedSize(curSize - 1);
    }
}
